let app = getApp()
import {
  getUserProfile,
  changeInfo
} from '../../../utils/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    fileList: [{
      url: 'https://thirdwx.qlogo.cn/mmopen/vi_32/ibDyX54QxYA2bA4hibT6Iib81qdQ0mEwkZkohTTFrebJmzbbm3nHG5l3jGZHFuPdQ3ibo3hkVsveZsmsZVpLWticSJg/132',
      name: '图片1',
      type: 'image'
    }],
    imgUrls: [],
  },
  bindDateChange: function (e) {
    let file = this.data.list;
    file.birthday = e.detail.value
    this.setData({
      list: file
    })
  },
  afterRead: function (event) {
    const {
      file
    } = event.detail; // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    this.setData({
      fileList: file
    })
    this.uploadImg(file);
  },
  delete(event) {
    console.log(event)
    let imgDelIndex = event.detail.index
    let fileList = this.data.fileList
    fileList.splice(imgDelIndex, 1)
    console.log('删除图片的', fileList)
    this.setData({
      fileList
    })
  },
  uploadImg(file) {
    let _this = this
    let userId = app.globalData.userId;
    const TOKEN = wx.getStorageSync('TOKEN')
    let url = file[0].url
    wx.uploadFile({
      url:  app.globalData.ip + '/user/photo?userId='+userId, 
      method:'POST',
      filePath: url,
      name: 'file',
      header: {
        'Authorization':TOKEN,
        'content-type': "multipart/form-data"
      },
      success (res){
        console.log('sucess',res);
      },
      fail(err){
        console.log('error',err);
      }
    })
  },
  onChange(event) {
    let radio = this.data.list;
    radio.gender = event.detail,
    this.setData({
      list: radio,
    });

  },
  //获取用户信息
  getUserProfile() {
    let userId = app.globalData.userId;
    getUserProfile(userId).then((res) => {
      console.log('获取用户信息',res);
      let file =this.data.fileList;
      file[0].type ="image";
      file[0].url = res.data.profilePhoto;
      file[0].name="图片1";
      console.log('file',file);
      this.setData({
        list: res.data.userProfile,
        fileList:file
      })
    })
  },

  realName(e){
    let realName = this.data.list
    realName.realName = e.detail.value;
    this.setData({
      list:realName
    })
  },
  idCard(e){
    let idCard = this.data.list
    idCard.idNumber = e.detail.value;
    this.setData({
      list:idCard
    })
  },
  area(e){
    let area = this.data.list
    area.area = e.detail.value;
    this.setData({
      list:area
    })
  },
  company(e){
    let company = this.data.list
    company.company = e.detail.value;
    this.setData({
      list:company
    })
  },
  career(e){
    let career = this.data.list
    career.career = e.detail.value;
    this.setData({
      list:career
    })
  },
  onLoad: function (options) {
    this.getUserProfile()
  },
  editor(e){
    let list = this.data.list
    list = JSON.stringify(list)
    const userId = app.globalData.userId
    changeInfo(userId,list).then((res)=>{
      console.log('提交成功',res);
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration:1000
      })
    })

  }
})